<?php

namespace app\admin\controller;

use think\Db;
use think\exception\DbException;
use think\facade\Url;
use think\response\Json;
use think\Controller;
use app\admin\model\Permission as PermissionModel;

class Permission extends Controller
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            $list = PermissionModel::order('sort','asc')->order('id','desc')->select();
            $data = [
                'code' => 0,
                'msg' => '正在请求中...',
                'count' => $list->count(),
                'data' => $list->toArray()
            ];
            return Json::create($data);
        }
        return $this->fetch();
    }

    /**
     * 显示创建资源表单页.
     * @return \think\Response
     */
    public function add()
    {
        $permissions = get_tree(PermissionModel::order('sort','asc')->select()->toArray());
        if ($this->request->isAjax()){
            $data = $this->request->param();
            $result = $this->validate($data,'app\admin\validate\Permission');
            if ($result !== true){
                $this->error($result);
            }
            try{
                PermissionModel::create($data);
            }catch (DbException $exception){
                $this->error('添加失败',null,$exception->getMessage());
            }
            $this->success('添加成功',Url::build('index'));
        }
        $this->assign('permissions',$permissions);
        return $this->fetch();
    }

    public function edit()
    {
        $id = $this->request->param('id');
        $permission = PermissionModel::getOrFail($id);
        $permissions = get_tree(PermissionModel::order('sort','asc')->select()->toArray());
        if ($this->request->isAjax()){
            $data = $this->request->param();
            $result = $this->validate($data,'app\admin\validate\Permission');
            if ($result !== true){
                $this->error($result);
            }
            try{
                $permission->save($data);
            }catch (DbException $exception){
                $this->error('更新失败',null,$exception->getMessage());
            }
            $this->success('更新成功',Url::build('index'));
        }
        $this->assign('permissions',$permissions);
        $this->assign('model',$permission);
        return $this->fetch();
    }

    /**
     * 删除用户，软删除
     */
    public function del()
    {
        $id = $this->request->param('id');
        $permission = PermissionModel::with('child')->findOrEmpty($id);
        if ($permission->isEmpty()){
            $this->error('数据不存在');
        }
        if (isset($permission->child) && !$permission->child->isEmpty()){
            $this->error('存在子权限，禁止删除');
        }
        //开启事务删除
        Db::startTrans();
        try{
            Db::name('role_permission')->where('permission_id',$id)->delete();
            Db::name('user_permission')->where('permission_id',$id)->delete();
            Db::name('permission')->delete($id);
            Db::commit();
        }catch (DbException $exception){
            Db::rollback();
            $this->error('删除失败');
        }
        $this->success('删除成功');
    }

}
